<?php

namespace App\Api\Dao;

use App\Exceptions\DaoException;
use App\Helpers\ResponseEnum;
use App\Models\TongUserBank;

class UserBankDao extends BaseDao
{

    /**
     * 查询构造器
     * @param $data
     * @return \Illuminate\Database\Eloquent\Builder
     */
    function getBuildQuery($data)
    {
        // TODO: Implement getBuildQuery() method.
        $query = TongUserBank::query();
        if(checkIsHasField($data,'id')){
            $query->where('id',$data['id']);
        }
        if(checkIsHasField($data,'u_id')){
            $query->where('u_id',$data['u_id']);
        }
        return $query;
    }

    /**
     * 添加银行卡
     * @param array $data
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|void
     */
    public function addBank(array $data)
    {
        $result = TongUserBank::query()->create($data);
        if(!$result){
            throw new DaoException(ResponseEnum::HTTP_ERROR,'新增失败');
        }
        return $result;
    }

    /**
     * 获取用户的银行卡列表
     * @param array $data
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|void
     */
    public function getBankList(array $data)
    {
        return $this->getBuildQuery($data)->get();
    }

    /**
     * @param array $data
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|void
     */
    public function bankInfo(array $data)
    {
        return $this->getBuildQuery($data)->first();
    }

    /**
     * 删除银行卡
     * @param array $data
     * @return void
     */
    public function delBank(array $data)
    {
        $result = TongUserBank::query()->where('id',$data['id'])->where('u_id',$data['u_id'])->delete();
        if(!$result){
            throw new DaoException(ResponseEnum::HTTP_ERROR,'新增失败');
        }
        return $result;
    }

    /**
     * @param array $data
     * @return void
     */
    public function editBank(array $data)
    {
        $u_id = $data['u_id'];
        $id = $data['id'];
        unset($data['id']);
        unset($data['u_id']);
        $result = TongUserBank::query()->where('id',$id)->where('u_id',$u_id)->update($data);
        if(!$result){
            throw new DaoException(ResponseEnum::HTTP_ERROR,'编辑失败');
        }
        return $result;
    }


}
